package org.ipower.platform.cfg;

import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/*
 * 解析Xml到对象。
 * */
public class ModuleDefineFactoryParse {
    private DocumentBuilderFactory factory = null;
    private DocumentBuilder builder = null;
    /*
     * 构造函数。
     * */
    public ModuleDefineFactoryParse(){
            this.factory = DocumentBuilderFactory.newInstance();
    }
    /*
     * 解析Xml文档。
     * */
    public ModuleDefineFactory Parse(InputStream inputStream){
        ModuleDefineFactory moduleDefineFactory = null;
        if(inputStream != null){
            try{
                    this.builder = this.factory.newDocumentBuilder();
                    Document doc = this.builder.parse(inputStream);
                    Element root = doc.getDocumentElement();
                    if(root != null && root.getNodeName().equalsIgnoreCase("ipower")){
                        moduleDefineFactory = new ModuleDefineFactory();
                        moduleDefineFactory.setVer(root.getAttribute("verion"));
                        NodeList sysList = root.getChildNodes();
                        if(sysList != null && sysList.getLength() > 0){
                                ModuleSystemDefineCollection  collection = new ModuleSystemDefineCollection();
                                for(int i = 0; i< sysList.getLength(); i++){
                                        Node node = sysList.item(i);
                                        if(node.getNodeType() ==  Node.ELEMENT_NODE){
                                                ModuleSystemDefine sysDefine = this.SystemDefineParse((Element)node);
                                                if(sysDefine != null){
                                                        collection.add(sysDefine);
                                                }
                                        }
                                }
                                if(collection.size() > 0){
                                        moduleDefineFactory.setSystem(collection);
                                }
                        }
                    }
            }catch(ParserConfigurationException e){
                    e.printStackTrace();
            }catch(SAXException e){
                    e.printStackTrace();
            }catch(IOException e){
                    e.printStackTrace();
            }
        }
        return moduleDefineFactory;
    }
    /*
     * 解析系统配置。
     * */
    protected ModuleSystemDefine SystemDefineParse(Element element){
        if(element != null && element.getNodeName().equalsIgnoreCase("System")){
                ModuleSystemDefine sys = new ModuleSystemDefine();
                sys.setSystemID(element.getAttribute("SystemID"));
                sys.setSystemSign(element.getAttribute("SystemSign"));
                sys.setSystemName(element.getAttribute("SystemName"));
                sys.setSystemDescription(element.getAttribute("SystemDescription"));
                NodeList list = element.getChildNodes();
                if(list != null && list.getLength() > 0){
                        ModuleDefineCollection collection = new ModuleDefineCollection();
                        for(int i = 0; i < list.getLength(); i++){
                                Node node = list.item(i);
                                if(node.getNodeType() ==  Node.ELEMENT_NODE){
                                        ModuleDefine module = this.ModuleDefineParse((Element)node);
                                        if(module != null){
                                                collection.add(module);
                                        }
                                }
                        }
                        if(collection.size() > 0){
                                sys.setModules(collection);
                        }
                }
                return sys;
        }
        return null;
    }
    /*
     * 解析模块配置。
     * */
    protected ModuleDefine ModuleDefineParse(Element element){
        if(element != null && element.getNodeName().equalsIgnoreCase("Module")){
                ModuleDefine m = new ModuleDefine();
                m.setModuleID(element.getAttribute("ModuleID"));
                m.setModuleName(element.getAttribute("ModuleName"));
                m.setModuleUri(element.getAttribute("ModuleUri"));
                String s = element.getAttribute("OrderNo");
                if(s != null && s.length() > 0){
                        m.setOrderNo(Integer.parseInt(s));
                }
                NodeList list = element.getChildNodes();
                if(list != null && list.getLength() > 0){
                        for(int i = 0; i < list.getLength(); i++){
                                Node node = list.item(i);
                                if(node.getNodeType() ==  Node.ELEMENT_NODE){
                                        ModuleDefine d = this.ModuleDefineParse((Element)node);
                                        if(d != null){
                                                m.getModules().add(d);
                                        }
                                }
                        }
                }
                return m;
        }
        return null;
    }
}